<?php
/*
 * @Description:
 * @Author: QinTianyu
 * @Date: 2020-04-24 11:18:02
 * @LastEditors: QinTianyu
 * @LastEditTime: 2020-05-09 14:43:01
 */
namespace app\shouyin\model;

use think\Model;
use app\product\model\Project as ProjectModel;
use app\product\model\Product as ProductModel;
use think\Db;
class UserCardLog extends Model
{
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    public function getProTextAttr($value,$data)
    {
        // dump($data);
        if($data['type'] == 1){  //项目
            $ProjectModel = new ProjectModel;
            return $ProjectModel->where('id', $data['pid'])->value('name');
        }else{ // 产品
            $ProductModel = new ProductModel;
            return $ProductModel->where('id', $data['pid'])->value('name');
        }
    }
    public function getTypeTextAttr($value, $data)
    {
        $type = ['1'=>'项目','2'=>'产品'];
        return $type[$value];
    }
    public function getPriceAttr($value,$data)
    {
        if($data['type'] == 1){  //项目
            $ProjectModel = new ProjectModel;
            return $ProjectModel->where('id', $data['pid'])->value('price');
        }else{ // 产品
            $ProductModel = new ProductModel;
            return $ProductModel->where('id', $data['pid'])->value('price');
        }
    }
    public function getUserNameAttr($value)
    {
        $name = Db::name('user')->where('id',$value)->value('nick');
        return $name;
    }
    public function hasUser()
    {
        return $this->hasOne('app\user\model\User', 'id', 'user_id');
    }
    public function hasCourse()
    {
        return $this->hasOne('Course', 'id', 'pid');
    }
    /**
     * @description: 获取用户当前会员指定疗程卡数量
     * @param {$userid, $pid}
     * @return:
     * @author: QinTianyu
     */
    public static function getUserCards($userid,$pid,$type=1)
    {

        $where = [
            ['pid','=',$pid],
            ['user_id','=',$userid],
            ['type','=',$type],
            ['use','=',1]
        ];
        $projectCardCount = self::where($where)->count('id');
        return $projectCardCount;
    }
}
